//
//  GRRiceOrderTableViewCell.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/9/7.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRRiceOrderTableViewCell.h"
#import "GRVerificationCodeView.h"
@implementation GRRiceOrderTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

-(void)setOrderModel:(GRRiceOrderModel *)orderModel{
    _orderModel = orderModel;
    self.nickNameLabel.text = orderModel.onePayName;
    self.countLabel.text =[NSString stringWithFormat:@"x%ld",orderModel.number] ;
    self.goodNamelLabel.text = orderModel.title;
     NSMutableAttributedString * attributingStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%.2lf",self.orderModel.totalPrice] attributes:@{NSForegroundColorAttributeName:HEXCOLOR(0x999999),NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]}];
    self.orignialLabel.attributedText = attributingStr ;
    self.priceLabel.text = [NSString stringWithFormat:@"￥%.2lf",self.orderModel.discountPrice];
    self.actPriceLabel.text =[NSString stringWithFormat:@"￥%.2lf",self.orderModel.payMoney];
    if ([orderModel.state isEqualToString: @"use"]) {
            self.orderIdLabel.text = orderModel.code;
    }else{
        if (orderModel.code) {
        NSMutableAttributedString * attributing = [[NSMutableAttributedString alloc]initWithString:orderModel.code attributes:@{NSForegroundColorAttributeName:HEXCOLOR(0x999999),NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]}];
        self.orderIdLabel.attributedText = attributing;
        }
    }


    [self.statusLabel setTitle:orderModel.stateStr forState:UIControlStateNormal];
    [self.goodIamgeView sd_setImageWithURL:[NSURL URLWithString:orderModel.mainPhoto] placeholderImage:[UIImage imageNamed:@""]];
    if ([orderModel.state isEqualToString: @"use"]) {
        self.codeBt.hidden = NO;
    }else{
        self.codeBt.hidden = YES;
    }
    self.leftBt.hidden = NO;
    self.rightBt.hidden = NO;
    self.codeTitleLabel.hidden = NO;
    self.orderIdLabel.hidden = NO;
    if ([orderModel.state isEqualToString: @"pay"] ) {//待支付
        [self.leftBt setTitle:@"取消订单" forState:UIControlStateNormal];
        [self.rightBt setTitle:@"去付款" forState:UIControlStateNormal];
        
    }else if([orderModel.state isEqualToString: @"use"]){//使用
        [self.leftBt setTitle:@"联系客服" forState:UIControlStateNormal];
        [self.rightBt setTitle:@"申请退款" forState:UIControlStateNormal];
        
    }else if ([orderModel.state isEqualToString:@"complete"]){//已消费
        self.leftBt.hidden = YES;
        [self.rightBt setTitle:orderModel.isComment? @"评价":@"已评价" forState:UIControlStateNormal];
        
    }else if ([orderModel.state isEqualToString:@"refund"]){//退款
        self.footView.hidden = YES;
        
    }else if([orderModel.state isEqualToString:@"expired"]){//过期
            self.footView.hidden = YES;
    }else{//取消
            self.footView.hidden = YES;
        self.codeTitleLabel.hidden = YES;
        self.orderIdLabel.hidden = YES;
    }

}

- (IBAction)LeftAction:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"联系客服"]) {
        if (_delegate && [_delegate respondsToSelector:@selector(detailOrderWithOrderType: orderModel:)]) {
            [_delegate detailOrderWithOrderType:RiceOrderActionTypeCustomerService orderModel:self.orderModel];
        }
    }else{//取消订单
        if (_delegate && [_delegate respondsToSelector:@selector(detailOrderWithOrderType: orderModel:)]) {
            [_delegate detailOrderWithOrderType:RiceOrderActionTypeCancleOrder orderModel:self.orderModel];
        }
    }
    
    
}

- (IBAction)rightAction:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"去付款"]) {
        if (_delegate && [_delegate respondsToSelector:@selector(detailOrderWithOrderType: orderModel:)]) {
            [_delegate detailOrderWithOrderType:RiceOrderActionTypePayOrder orderModel:self.orderModel];
        }
    }else if([sender.titleLabel.text isEqualToString:@"申请退款" ]){//取消订单
        if (_delegate && [_delegate respondsToSelector:@selector(detailOrderWithOrderType: orderModel:)]) {
            [_delegate detailOrderWithOrderType:RiceOrderActionTypeReturnMoeny orderModel:self.orderModel];
        }
        
    }else if([sender.titleLabel.text isEqualToString:@"评价"]){
        if (_delegate && [_delegate respondsToSelector:@selector(detailOrderWithOrderType: orderModel:)]) {
            [_delegate detailOrderWithOrderType:RiceOrderActionTypeComment orderModel:self.orderModel];
        }
    }
}

- (IBAction)showCodeView:(id)sender {
    GRVerificationCodeView  * codeView = [ GRVerificationCodeView xx_instantiateFromNib];
    [codeView showCodeView: [NSString stringWithFormat:@"验证码%@",self.orderModel.code] QrCodeUrl:self.orderModel.qrCode];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
